package cn.jarlen.jwt.tools.antivirus;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import cn.jarlen.jwt.tools.menu.IMenuProcess;

/**
 * @author jarlen
 * Create by 2024/7/4 8:49
 */
public class ExportCVELib implements IMenuProcess {

    @Override
    public void onExecute(Context context) {
        Log.w("ResetCVELib", "导出漏洞库 开始");
        exportDatabase2Sdcard(context.getApplicationContext());
        Log.w("ResetCVELib", "导出漏洞库 完成");
    }
    
    public String exportDatabase2Sdcard(Context context) {
        String sourceFile = context.getDatabasePath("antivirus").getPath() + ".db";
        String distFile = Environment.getExternalStorageDirectory() + "/antivirus.db";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(distFile);
            try {
                inputStream = new FileInputStream(sourceFile);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = inputStream.read(buffer)) > 0)
                    outputStream.write(buffer, 0, length);
                outputStream.flush();
                return distFile;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
